// --------------------------------------------------------------------------------------------------------------------
// <copyright file="UIInstrumentList.cs" company="Open Trader">
//   Copyright (c) David Denis (david.denis@systemathics.com)
// </copyright>
// <summary>
//   |  Open Trader - The Open Source Systematic Trading Platform
//   |
//   |  This program is free software: you can redistribute it and/or modify
//   |  it under the terms of the GNU General Public License as published by
//   |  the Free Software Foundation, either version 2 of the License, or
//   |  (at your option) any later version.
//   |
//   |  This program is distributed in the hope that it will be useful,
//   |  but WITHOUT ANY WARRANTY; without even the implied warranty of
//   |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//   |  GNU General Public License for more details.
//   |
//   |  You should have received a copy of the GNU General Public License
//   |  along with this program.  If not, see http://www.gnu.org/licenses
//   |
//   |  Up to date informations about Open Trader can be found at :
//   |    http://opentrader.org
//   |    http://opentrader.codeplex.com
//   |
//   |  For professional services, please visit us at :
//   |    http://www.systemathics.com
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Org.OpenTrader.Framework.Forge.UI.Forms
{
    #region Using Directives

    using System.Collections.Generic;
    using System.ComponentModel;

    using Org.OpenTrader.Framework.Events;
    using Org.OpenTrader.Framework.Forge.Interfaces;
    using Org.OpenTrader.Framework.UI.Forms;

    #endregion

    /// <summary>
    /// The ui instrument list.
    /// </summary>
    public partial class UIInstrumentStaticDataList : UIControl
    {
        #region Constants and Fields

        /// <summary>
        /// The instruments.
        /// </summary>
        private IList<IInstrumentStaticData> instruments = new List<IInstrumentStaticData>();

        #endregion

        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="UIInstrumentStaticDataList"/> class.
        /// </summary>
        public UIInstrumentStaticDataList()
        {
            this.InitializeComponent();
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets Instruments.
        /// </summary>
        [Browsable(false)]
        public IList<IInstrumentStaticData> Instruments
        {
            get
            {
                return this.instruments;
            }

            set
            {
                this.instruments = value;
            }
        }

        /// <summary>
        /// Gets UIVirtualGrid.
        /// </summary>
        [Browsable(false)]
        public UIVirtualGrid UIVirtualGrid
        {
            get
            {
                return this.virtualGrid;
            }
        }

        #endregion

        #region Methods

        /// <summary>
        /// The virtualGrid_ ui grid query cell info.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridQueryCellInfo(object sender, VirtualGridEventArgs.QueryCellInfo e)
        {
            if (e.RowIndex >= this.instruments.Count || e.RowIndex < 0)
            {
                return;
            }

            var i = this.instruments[e.RowIndex];

            // According the column display required data
            switch (e.ColumnIndex)
            {
                case 0:
                    e.Value = i.Code;
                    e.ToolTip = string.Format("ID={0}\nISIN={1}\nName={2}", i.ID, i.ISIN, i.Name);
                    break;

                case 1:
                    e.Value = i.TradingScope;
                    break;

                case 2:
                    e.Value = i.Market.Path;
                    break;

                case 3:
                    e.Value = i.TickSizeRule;
                    break;

                case 4:
                    e.Value = i.Type;
                    e.ToolTip = i.GetType().ToString();
                    break;

                case 5:
                    e.Value = i.Currency;
                    break;
            }
        }

        /// <summary>
        /// The virtualGrid_ ui grid query row count.
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void OnVirtualGridQueryRowCount(object sender, VirtualGridEventArgs.QueryRowCount e)
        {
            if (this.instruments == null)
            {
                e.RowCount = 0;
                return;
            }

            e.RowCount = this.instruments.Count;
        }

        #endregion
    }
}